<?php

namespace App\Http\Controllers\Api\Utils;

use App\Http\Controllers\Controller;
use Geotools\Coordinate\Coordinate;
use Geotools\Geohash\Geohash;
use Geotools\Geotools;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class LonLatController extends Controller
{
    // 根据用户当前位置的经纬度获取附近数据
    public function mapRestaurantBubbles(Request $request): array
    {
        // 平安科技金融大厦经纬度
        $lon = 114.337175;
        $lat = 30.555052;
        $coordinates = [$lat, $lon];
        $coordinate = new Coordinate($coordinates);
        $geoTools = new Geotools();
        $encoded = $geoTools->geohash()->encode($coordinate, 12);
        $geoHash = substr($encoded->getGeohash(), 0, 6);

        // $scale = 12; // 默认附近 2km 的数据
        // $scale = 11; // 附近 5km 的数据

        $input = $request->input();
        if (isset($input['scale'])) {
            if ($input['scale'] == 11) { // 代表用户将地图缩小了，我们就查询街道级别的数据
                $geoHash = substr($encoded->getGeohash(), 0, 4);
                return DB::table('t_region', 't1')
                    ->leftJoin('t_restaurant as t2', 't1.id', '=', 't2.region_id')
                    ->leftJoin('t_street as t3', 't1.street_id', '=', 't3.id')
                    ->where('t2.geohash', 'like', "$geoHash%")
                    ->select('t1.id as region_id', 't3.name', DB::raw('count(t1.id) as recruit_count'))
                    ->groupBy('t3.id')
                    ->get()
                    ->toArray();
            } else if ($input['scale'] == 9) { // 查询区级别数据
                $geoHash = substr($encoded->getGeohash(), 0, 3);
                return DB::table('t_region', 't1')
                    ->leftJoin('t_restaurant as t2', 't1.id', '=', 't2.region_id')
                    ->leftJoin('t_street as t3', 't1.street_id', '=', 't3.id')
                    ->leftJoin('t_district as t4', 't3.district_id', '=', 't4.id')
                    ->where('t2.geohash', 'like', "$geoHash%")
                    ->select('t4.id as region_id', 't4.name', DB::raw('count(t1.id) as recruit_count'))
                    ->groupBy('t4.id')
                    ->get()
                    ->toArray();
            }
        }

        return DB::table('t_region', 't1')
            ->leftJoin('t_restaurant as t2', 't1.id', '=', 't2.region_id')
            ->where('t2.geohash', 'like', "$geoHash%")
            ->select('t1.id as region_id', 't1.name', DB::raw('count(t1.id) as recruit_count'))
            ->groupBy('t1.id')
            ->get()
            ->toArray();
    }

    /**
     * 根据区域 id 获取当前区域下的列表数据
     * @param Request $request
     * @return JsonResponse|Collection
     */
    public function restaurantBubbleList(Request $request): JsonResponse|Collection
    {
        $rule = [
            "region_id" => "required|integer"
        ];
        $input       = $request->input();
        $validateRet = Validator::make($input, $rule);
        if ($validateRet->fails()) {
            return result(1, $validateRet->errors()->first());
        }

        return DB::table('t_restaurant')
            ->where('region_id', '=', $input['region_id'])
            ->get('name');
    }


}
